import { reqAddOrUpdateShopCart, reqGoodsInfo } from "@/api"
import {getUUID} from '@/utils/uuid_token'
const state={
  goodInfo:{},
  //通过getUUID（）的方式使uuid的值保持不变
  uuid_token:getUUID()
}
const mutations={
  GETGOODINFO(state,goodInfo){
    state.goodInfo=goodInfo
  }
}
const actions={
  async getGoodInfo({commit},skuId){
    let result= await reqGoodsInfo(skuId)
    if(result.code==200){
      commit('GETGOODINFO',result.data)
    }
  },
  // 将产品添加到购物车中
  //因为服务器没有返回其他数据，因此不用三连环
  //async修饰返回的是一个promis对象【要么成功，要么失败】
  //await只接收promise对象返回的成功的结果与.then类似，axios返回的是promise对象，所以可以使用await进行修饰
  async addOrUpdateShopCart({commit},{skuId,skuNum}){
     let result=await reqAddOrUpdateShopCart(skuId,skuNum)
    //  console.log(result.then(value=>{console.log(value)}))
     if(result.code==200){
      //因为addOrUpdateShopCart不是由new Promise实例创建，所以得添加 return Promise.resolve||reject
      return Promise.resolve("ok")
      
     }
     else{
      return Promise.reject(new Error('faile'))
     }
  }

}
const getters={
  categoryView(state){
    return state.goodInfo.categoryView||{};
  },
  skuInfo(state){
    return state.goodInfo.skuInfo||{}
  },
  spuSaleAttrList(state){
    return state.goodInfo.spuSaleAttrList||[]
  }
}
export default{
  state,
  actions,
  mutations,
  getters
}